/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace EPiServer.Research.Wiki.Properties
{
    /// <summary>
    /// PropertyControl implementation used for rendering PropertyAccessLevel data.
    /// </summary>
    public class PropertyAccessLevelControl : EPiServer.Web.PropertyControls.PropertyStringControl
    {

        private DropDownList ddl;
        public override void CreateEditControls()
        {
            ddl = new DropDownList();
            ddl.DataSource = Enum.GetNames(typeof(EPiServer.Security.AccessLevel)).Union(new List<string>(){"Logged in"});
            ddl.DataBind();
            Controls.Add(ddl);
            this.SetupEditControls();
        }

        protected override void SetupEditControls()
        {
            if (!string.IsNullOrEmpty((string)PropertyAccessLevel.Value))
                ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(PropertyAccessLevel.Value.ToString()));

        }

        public override void ApplyEditChanges()
        {
            PropertyAccessLevel.Value = ddl.SelectedValue;
        }

        /// <summary>
        /// Gets the PropertyAccessLevel instance for this IPropertyControl.
        /// </summary>
        /// <value>The property that is to be displayed or edited.</value>
        public PropertyAccessLevel PropertyAccessLevel
        {
            get
            {
                return PropertyData as PropertyAccessLevel;
            }
        }
    }
}
